(in-package #:cl-fast-ecs/tests)

(define-test cl-fast-ecs)

(defparameter *enable-colors* NIL)

(defun run ()
  (clear-coverage)
  (let* ((interactivep (or (find :slynk *features*)
                           (find :swank *features*)))
         (*enable-colors* (not interactivep)))
    (test (if uiop:*command-line-arguments*
              (mapcar (lambda (a)
                        (alexandria:format-symbol
                         'cl-fast-ecs/tests "~:@(~a~)" a))
                      uiop:*command-line-arguments*)
              'cl-fast-ecs)
          :report (if interactivep 'interactive 'plain))))

(defparameter *regular* 39)
(defparameter *green* 32)
(defparameter *red* 31)

(defun color-text (color text)
  (if *enable-colors*
      (format nil "[0;~am~a[0;39m" color text)
      text))

(defun status-text (result text)
  (color-text
   (case (status result)
     (:passed *green*)
     (:failed *red*)
     (otherwise *regular*))
   text))

(defmethod parachute:format-result :around ((result result)
                                            (type (eql :oneline)))
  (status-text result (call-next-method)))

(defmethod parachute:format-result :around ((result test-result)
                                            (type (eql :oneline)))
  (let ((*print-case* :downcase)
        (name (name (expression result))))
    (status-text result (format NIL "~a" name))))

#+(and sbcl (not ecs-release))
(defmethod parachute:summarize :around ((report report))
  (call-next-method)
  (when (uiop:getenvp "COVERAGE")
    (handler-bind ((warning #'muffle-warning))
      (create-coverage-report "coverage/")
      (pprint-coverage
       (get-coverage "coverage/"
                     :exclude '("main.lisp"
                                "package.lisp"
                                "test-component-default.lisp"
                                "test-component-redefinition.lisp"
                                "test-component-ro.lisp"
                                "test-component-shadow.lisp"
                                "test-components.lisp"
                                "test-components-system-packages.lisp"
                                "test-component+system.lisp"
                                "test-component-system-ro.lisp"
                                "test-entities.lisp"
                                "test-hooks.lisp"
                                "test-storage.lisp"
                                "test-systems-load-order.lisp"
                                "test-systems-order.lisp"
                                "test-systems.lisp"))))))
